import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.xml.parsers.*;
import org.w3c.dom.*;

public class User {
	/*
	 * data about the user
	 */
	public String user_name;

	public Dict now_dict;

	public int now_word;
	
	public int max_learn;
	
	public int new_word_probability;

	public String rem_seq[];

	public String note[];

	public int hit[];

	public int miss[];

	public int easy[];
	
	/*
	 * get the learning record of user
	 */
	public void get_record(String _user_name) throws Exception {
		File f = new File(_user_name + ".xml");
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.parse(f);

		user_name = doc.getElementsByTagName("user").item(0).getAttributes()
				.getNamedItem("user_name").getTextContent();

		now_dict = new Dict();
		now_dict.init_dict(doc.getElementsByTagName("user").item(0)
				.getAttributes().getNamedItem("now_dict").getTextContent());

		now_word = Integer.valueOf(
				doc.getElementsByTagName("user").item(0).getAttributes()
						.getNamedItem("now_word").getTextContent()).intValue();

		max_learn = Integer.valueOf(
				doc.getElementsByTagName("user").item(0).getAttributes()
						.getNamedItem("max_learn").getTextContent()).intValue();

		new_word_probability = Integer.valueOf(
				doc.getElementsByTagName("user").item(0).getAttributes()
						.getNamedItem("new_word_probability").getTextContent()).intValue();

		
		rem_seq = new String[now_dict.cnt_words];
		note = new String[now_dict.cnt_words];
		hit = new int[now_dict.cnt_words];
		miss = new int[now_dict.cnt_words];
		easy = new int[now_dict.cnt_words];

		NodeList word = doc.getElementsByTagName("word");
		for (int i = 0; i < word.getLength(); ++i) {
			rem_seq[i] = word.item(i).getAttributes().getNamedItem("rem_seq")
					.getTextContent();
			
			note[i] = word.item(i).getAttributes().getNamedItem("note")
					.getTextContent();
			
			easy[i] = Integer.valueOf(
					word.item(i).getAttributes().getNamedItem("easy")
							.getTextContent()).intValue();
			hit[i] = 0;
			miss[i] = 0;
			for (int j = 0; j < rem_seq[i].length(); ++j) {
				if (rem_seq[i].charAt(j) == 'o') {
					++hit[i];
				} else if (rem_seq[i].charAt(j) == 'x') {
					++miss[i];
				}
			}

		}
	}

	/*
	 * new learning record
	 */
	public void new_record(String _user_name) throws IOException {
		user_name = _user_name;
		now_dict = new Dict();
		now_dict.dict_name = "GRE";
		now_dict.init_dict(now_dict.dict_name);
		now_word = 0;
		max_learn = 100;
		new_word_probability = 30;
		
		rem_seq = new String[now_dict.cnt_words];
		note = new String[now_dict.cnt_words];
		easy = new int[now_dict.cnt_words];
		
		for (int i = 0; i < now_dict.cnt_words; ++i) {
			rem_seq[i] = "";
			note[i] = "";
		}

		save_record();
	}

	/*
	 * save learning record
	 */
	public void save_record() throws IOException {
		FileWriter fw = new FileWriter(user_name + ".xml");
		PrintWriter out = new PrintWriter(fw);
		out.printf("<user user_name=\"%s\" now_dict=\"%s\" now_word=\"%d\" max_learn=\"%d\" new_word_probability=\"%d\">\n",
				user_name, now_dict.dict_name, now_word, max_learn, new_word_probability);
		for (int i = 0; i < now_dict.cnt_words; i++) {
			out.printf("<word rem_seq=\"%s\" note=\"%s\" easy=\"%d\"></word>\n",
					rem_seq[i], note[i], easy[i]);
		}
		out.printf("</user>\n");
		fw.close();
		out.close();
	}

	/*
	 * get the next word
	 */
	public int get_next(int p_new) {
		boolean get_new = false;
		int t = (int) (Math.random() * 100);
		if (t <= p_new)
			get_new = true;

		if (!get_new) {
			int id[] = new int[now_dict.cnt_words];
			int len_id = 0;
			for (int i = 0; i < now_dict.cnt_words; ++i) {
				if (hit[i] + miss[i] != 0 && easy[i] == 0) {
					id[len_id++] = i;
				}
			}
			return id[(int) (Math.random() * (len_id - 1))];
		} else {
			return now_word++;
		}
	}

	/*
	 * remember a word
	 */
	public void remember(int word_id) {

	}

	/*
	 * forget a word
	 */
	public void forget(int word_id) {
		rem_seq[word_id] = rem_seq[word_id] + "x";
		++miss[word_id];
	}

	/*
	 * correct
	 */
	public void correct(int word_id) {
		rem_seq[word_id] = rem_seq[word_id] + "o";
		++hit[word_id];
	}

	/*
	 * wrong
	 */
	public void wrong(int word_id) {
		this.forget(word_id);
	}

	/*
	 * easy
	 */
	public void easy(int word_id) {
		if (easy[word_id] == 0) {
			easy[word_id] = 1;
		}
	}

	/*
	 * save the note
	 */
	public void save_note(int word_id, String _note) {
		note[word_id] = _note;
	}
}
